package com.yunfeisoft.business.controller;

import com.yunfeisoft.base.enumeration.YesNoEnum;
import com.yunfeisoft.base.model.Attachment;
import com.yunfeisoft.base.model.User;
import com.yunfeisoft.base.service.inter.AttachmentService;
import com.yunfeisoft.business.model.Elder;
import com.yunfeisoft.business.model.ElderApprovalResult;
import com.yunfeisoft.business.service.inter.ElderApprovalResultService;
import com.yunfeisoft.business.service.inter.ElderService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.Page;
import com.yunfeisoft.common.utils.Response;
import com.yunfeisoft.common.utils.ResponseUtils;
import com.yunfeisoft.common.utils.Validator;
import com.yunfeisoft.utils.ApiUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: ElderController
 * Description: 老人入院登记表Controller
 * Author: Jackie liu
 * Date: 2020-01-13
 */
@RestController
public class ElderController extends BaseController {

    @Autowired
    private ElderService elderService;

    @Autowired
    private ElderApprovalResultService elderApprovalResultService;

    @Autowired
    private AttachmentService attachmentService;

    /**
     * 添加老人入院登记表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/elder/save", method = RequestMethod.POST)
    public Response save(Elder record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "name", "姓名为空");
        validator.required(request, "sex", "性别为空");
        validator.required(request, "birthDate", "出生年月为空");
        validator.required(request, "eduDegree", "文化程度为空");
        validator.required(request, "politicalOutlook", "政治面貌为空");
        validator.required(request, "nativePlace", "籍贯为空");
        validator.required(request, "nation", "民族为空");
        validator.required(request, "maritalStatus", "婚姻状况为空");
        validator.required(request, "profession", "曾从事职业为空");
        validator.required(request, "socialCardNum", "社保卡号为空");
        validator.required(request, "idcard", "身份证号为空");
        validator.required(request, "address", "户籍地址为空");
        validator.required(request, "birthday", "生日为空");
        validator.required(request, "contactName1", "第一联系人为空");
        validator.required(request, "contactPhone1", "第一联系人电话为空");
        validator.required(request, "incomeSource", "经济来源为空");
        validator.required(request, "faith", "宗教信仰为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        record.setStatus(Elder.ElderStatusEnum.REGISTER.getValue());

        User user = ApiUtils.getLoginUser();
        record.setOrgId(user.getOrgId());

        elderService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改老人入院登记表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/elder/modify", method = RequestMethod.POST)
    public Response modify(Elder record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "name", "姓名为空");
        validator.required(request, "sex", "性别为空");
        validator.required(request, "birthDate", "出生年月为空");
        validator.required(request, "eduDegree", "文化程度为空");
        validator.required(request, "politicalOutlook", "政治面貌为空");
        validator.required(request, "nativePlace", "籍贯为空");
        validator.required(request, "nation", "民族为空");
        validator.required(request, "maritalStatus", "婚姻状况为空");
        validator.required(request, "profession", "曾从事职业为空");
        validator.required(request, "socialCardNum", "社保卡号为空");
        validator.required(request, "idcard", "身份证号为空");
        validator.required(request, "address", "户籍地址为空");
        validator.required(request, "birthday", "生日为空");
        validator.required(request, "contactName1", "第一联系人为空");
        validator.required(request, "contactPhone1", "第一联系人电话为空");
        validator.required(request, "incomeSource", "经济来源为空");
        validator.required(request, "faith", "宗教信仰为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        elderService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询老人入院登记表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/elder/query", method = {RequestMethod.POST, RequestMethod.GET})
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        Elder record = elderService.load(id);
        // 查询老人审批记录列表
        List<ElderApprovalResult> approvalResultList = elderApprovalResultService.queryByElderId(id);
        if (!ObjectUtils.isEmpty(approvalResultList)) {
            for (ElderApprovalResult result : approvalResultList) {
                // 查询该审批记录的附件
                List<Attachment> attachments = attachmentService.queryByRefId(result.getId());
                result.setAttachmentList(attachments);
            }
        }
        Map<String, Object> data = new HashMap<>();
        data.put("elder", record);
        data.put("approvalResultList", approvalResultList);
        return ResponseUtils.success(data);
    }

    /**
     * 分页查询老人入院登记表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/elder/list", method = {RequestMethod.POST, RequestMethod.GET})
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String name = ServletRequestUtils.getStringParameter(request, "name", null);
        int nursingLevel = ServletRequestUtils.getIntParameter(request, "nursingLevel", -1);
        int status = ServletRequestUtils.getIntParameter(request, "status", -1);
        // 获取用户信息
        User user = ApiUtils.getLoginUser();
        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("name", name);
        // 操作人为当前登录用户
        params.put("createId", user.getId());
        if (status > 0) {
            params.put("status", status);
        }
        if (nursingLevel > 0) {
            params.put("nursingLevel", nursingLevel);
        }

        if (user.getIsSys() != YesNoEnum.YES_ACCPET.getValue()) {
            params.put("orgId", user.getOrgId());
        }
        Page<Elder> page = elderService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除老人入院登记表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/elder/delete", method = {RequestMethod.POST, RequestMethod.GET})
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        elderService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }

    /**
     * 获取所有老人列表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/elder/listAll", method = {RequestMethod.POST, RequestMethod.GET})
    public Response listAll(HttpServletRequest request, HttpServletResponse response) {
        String name = ServletRequestUtils.getStringParameter(request, "name", null);
        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("name", name);

        User user = ApiUtils.getLoginUser();
        if (user.getIsSys() == YesNoEnum.NO_CANCEL.getValue()) {
            params.put("orgId", user.getOrgId());
        }

        List<Elder> page = elderService.listAll(params);
        return ResponseUtils.success(page);
    }

    /**
     * 正式入住
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/elder/checkIn", method = RequestMethod.POST)
    public Response checkIn(Elder record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "roomId", "房间号为空");
        validator.required(request, "bedNum", "床位为空");
        validator.required(request, "code", "入院号为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        // 设置入住时间
        record.setCheckInDate(new Date());
        // 设置状态为入住
        record.setStatus(Elder.ElderStatusEnum.CHECK_IN.getValue());
        elderService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 老人入院财务通过
     * @param record
     * @param request
     * @param response
     * @return
     */
//    @RequestMapping(value = "/web/elder/financeApproval", method = RequestMethod.POST)
//    public Response financeApproval(String ids, HttpServletRequest request, HttpServletResponse response) {
//        if (StringUtils.isBlank(ids)) {
//            return ResponseUtils.warn("参数错误");
//        }
//        String[] idArr = ids.split(",");
//        Arrays.asList(idArr).stream().forEach(id -> {
//            Elder elder = new Elder();
//            elder.setId(id);
//            // 设置状态为财务审批通过
//            elder.setStatus(Elder.ElderStatusEnum.FINANCE_ACCEPT.getValue());
//            elderService.modify(elder);
//        });
//        return ResponseUtils.success("保存成功");
//    }

}
